package domain;

import java.util.Collection;
import java.util.Date;
import java.util.HashSet;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;

import org.springframework.format.annotation.DateTimeFormat;

@Entity
@Access(AccessType.PROPERTY)
@Table(name = "_order")
public class Order extends DomainEntity{

	//Constructors
	public Order(){
		super();
		purchasedItems = new HashSet<PurchasedItem>();
	}

	//Attributes
	private Date creationMoment;
	private Money totalPrice;
	
	@NotNull
	@Past
	@Temporal(TemporalType.TIMESTAMP)
	@DateTimeFormat(pattern = "dd/MM/yyyy HH:mm")
	public Date getCreationMoment() {
		return creationMoment;
	}
	public void setCreationMoment(Date creationMoment) {
		this.creationMoment = creationMoment;
	}
	@NotNull
	@Valid
	public Money getTotalPrice() {
		return totalPrice;
	}
	public void setTotalPrice(Money totalPrice) {
		this.totalPrice = totalPrice;
	}
	
	//Relationships
	private Collection<PurchasedItem> purchasedItems;
	private Customer buyer;
	
	@OneToMany(mappedBy = "order")
	public Collection<PurchasedItem> getPurchasedItems() {
		return purchasedItems;
	}
	public void setPurchasedItems(Collection<PurchasedItem> purchasedItems) {
		this.purchasedItems = purchasedItems;
	}
	
	@NotNull
	@Valid
	@ManyToOne(optional=false)
	public Customer getBuyer() {
		return buyer;
	}
	public void setBuyer(Customer buyer) {
		this.buyer = buyer;
	}
}
